# compile stage
FROM ccr.ccs.tencentyun.com/rootegg/node:pnpm-9.3.0 as build-stage

WORKDIR /appinstall

COPY package*.json pnpm-lock.yaml ./

RUN pnpm install --registry https://registry.npmmirror.com

COPY . .

RUN pnpm run build

# production stage
FROM ccr.ccs.tencentyun.com/rootegg/node:nginx-1.24.0 as production-stage
ARG API_SERVER
ENV LANG en_US.UTF-8
RUN echo -e " \n\
add_header Access-Control-Allow-Credentials true; \n\
add_header Access-Control-Allow-Origin *; \n\
add_header Access-Control-Allow-Headers 'x-requested-with,content-type,Cache-Control,Pragma,Date,x-timestamp'; \n\
add_header Access-Control-Allow-Methods 'POST,GET,OPTIONS,PUT,DELETE'; \n\
server {  \n\
    listen       80; \n\
    location   /api/ { \n\
        proxy_pass              ${API_SERVER}/api/; \n\
        proxy_set_header        X-Real-IP \$remote_addr; \n\
        proxy_set_header        X-Forwarded-For \$proxy_add_x_forwarded_for; \n\
    } \n\
    location / { \n\
        root   /var/www/html/; \n\
        index  index.html index.htm; \n\
        if (!-e \$request_filename) { \n\
            rewrite ^(.*)\$ /index.html?s=\$1 last; \n\
            break; \n\
        } \n\
    } \n\
    location   /cy-jeecg-ui/ { \n\
        alias   /var/www/html/; \n\
    } \n\
    keepalive_timeout  600; \n\
    fastcgi_connect_timeout 600; \n\
    fastcgi_send_timeout 600; \n\
    fastcgi_read_timeout 600; \n\
    proxy_connect_timeout 600; \n\
    proxy_send_timeout 600; \n\
    proxy_read_timeout 600; \n\
    send_timeout 600; \n\
    gzip on; \n\
    gzip_min_length 1k; \n\
    gzip_http_version 1.1; \n\
    gzip_comp_level 6; \n\
    gzip_types text/plain application/x-javascript text/css application/xml application/javascript; \n\
    gzip_vary on; \n\
    access_log  /var/log/nginx/access.log ; \n\
    client_max_body_size 100M; \n\
    client_body_buffer_size 100M; \n\
    } " > /etc/nginx/http.d/default.conf \
    &&  mkdir  -p  /var/www \
    &&  mkdir -p /var/www/html
COPY --from=build-stage /appinstall/dist /var/www/html/
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
